/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.graph;

import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides the implementation of 'caml_gr_point_color' primitive.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class PointCol {

    /**
     * No instance of this class.
     */
    private PointCol() {
    } // end empty constructor

    /**
     * Returns the color of a given point of the backing store.
     * @param ctxt context
     * @param vx x-component of the point coordinate
     * @param vy y-component of the point coordinate
     * @return the color of the point at <i>(vx, vy)</i>
     * @throws Fail.Exception if <i>Graphics</i> module has not been initialized
     */
    @Primitive
    public static Value caml_gr_point_color(final CodeRunner ctxt,
                                            final Value vx,
                                            final Value vy)
        throws Fail.Exception {
        final GraphSlot slot = GraphSlot.checkGraph(ctxt);
        return Value.createFromLong(slot.getBackingStore().getRGB(vx.asLong(), slot.convertY(vy.asLong())) & 0x00FFFFFF);
    } // end method 'caml_gr_point_color(CodeRunner, Value, Value)'

} // end class 'PointCol'
